﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InternshipManagement.Models;
using InternshipManagement.Helpers;

namespace InternshipManagement.Controllers
{
    public class CourseController : Controller
    {
        CourseRepository courseRepository = new CourseRepository();
        SemesterRepository semesterRepository = new SemesterRepository();
        //
        // GET: /Course/
        [CustomAuthorize(Privileges = UserPrivileges.ListCourses)]
        public ActionResult List(int? page)
        {
            const int pageSize = 10;

            IQueryable<Course> courses = null;

            courses = courseRepository.FindAllActiveCourses();

            var paginatedCourses = new PaginatedList<Course>(courses, page ?? 0, pageSize);

            return View(paginatedCourses);
        }

        [CustomAuthorize(Privileges = UserPrivileges.CreateCourse)]
        public ActionResult Create()
        {
            ViewBag.Op = "Create";
            ViewBag.Semesters = semesterRepository.FindAllActiveSemesters();
            return View("CreateEdit");
        }

        [HttpPost]
        [CustomAuthorize(Privileges = UserPrivileges.CreateCourse)]
        public ActionResult Create(Course course)
        {
            if (ModelState.IsValid)
            {
                if (!courseRepository.CourseExists(course.Code, course.SemesterID.Value))
                {
                    course.Status = "A";
                    courseRepository.AddCourse(course);
                    courseRepository.Save();

                    return RedirectToAction("List");
                }
                else
                {
                    ModelState.AddModelError("", "The course already exists.");
                }
            }
            ViewBag.Op = "Create";
            ViewBag.Semesters = semesterRepository.FindAllActiveSemesters();
            return View("CreateEdit", course);
        }

        [CustomAuthorize(Privileges = UserPrivileges.EditCourse)]
        public ActionResult Edit(int id)
        {
            Course course = courseRepository.GetCourse(id);
            if (course == null)
            {
                return View("ShowMessage", "Course does not exists");
            }
            ViewBag.Op = "Edit";
            ViewBag.Semesters = semesterRepository.FindAllActiveSemesters();
            return View("CreateEdit", course);
        }


        [HttpPost]
        [CustomAuthorize(Privileges = UserPrivileges.EditCourse)]
        public ActionResult Edit(int id, FormCollection formvalues)
        {

            Course course = courseRepository.GetCourse(id);
            if (TryUpdateModel(course, formvalues))
            {
                if (!courseRepository.CourseExists(course.Code, course.SemesterID.Value))
                {
                    courseRepository.Save();
                    return RedirectToAction("List");
                }
                else
                {
                    ModelState.AddModelError("", "The Course already exists.");
                }
            }
            ViewBag.Op = "Edit";

            ViewBag.Semesters = semesterRepository.FindAllActiveSemesters();
            return View("CreateEdit", course);
        }

        [CustomAuthorize(Privileges = UserPrivileges.DeleteCourse)]
        public ActionResult Delete(int id)
        {
            Course Course = courseRepository.GetCourse(id);
            Course.Status = "P";
            courseRepository.Save();
            return RedirectToAction("List");
        }

    }
}
